package com.woniu.carrent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.woniu.carrent.mapper.TUserMapper;
import com.woniu.carrent.mapper.TUserinfoMapper;
import com.woniu.carrent.pojo.TMenu;
import com.woniu.carrent.pojo.TUser;
import com.woniu.carrent.pojo.TUserinfo;
import com.woniu.carrent.service.TMenuService;
import com.woniu.carrent.service.TUserService;
import com.woniu.carrent.util.ResultMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser> implements TUserService {
    @Autowired
    private TUserMapper TUserMapper;
    @Autowired
    private TMenuService tMenuService;
    @Autowired
    private TUserinfoMapper tUserinfoMapper;
    /**
     * 用户登录方法
     */
    @Override
    public ResultMessage login(TUser TUser) {
        //1.根据用户名进行查询用户信息
        QueryWrapper<TUser> wrapper = new QueryWrapper<TUser>();
        //添加查询条件
        wrapper.eq("phone", TUser.getPhone());
        TUser user = TUserMapper.selectOne(wrapper);
        if(user.getIsdeleted()==1){
            return ResultMessage.fail("用户已被封禁", 531, user);
        }
        if (user != null) { //表示查询到指定用户
            //对用户输入密码进行加密
            String md5Pwd = DigestUtils.md5DigestAsHex(TUser.getPassword().getBytes());
            //判断用户密码是否正确
            if(user.getPassword().equals(md5Pwd)){
                //获取用户的菜单
                List<TMenu> menus = tMenuService.selectMenusByTypeId(user.getUtypeid());
                user.setMenus(menus);
                //表示密码相同
                return ResultMessage.success(user);
            }
        }
        return ResultMessage.fail("用户名或密码错误", 530, user);
    }
    /**
     * 根据id查询用户信息
     * @param userId
     */
    @Override
    public  TUser selectInfoByUserId(Integer userId){
        TUser user = TUserMapper.selectByUserId(userId);
        return user;
    }
}
